#include "kernel_operator.h"

constexpr int32_t BUFFER_NUM = 2;


class KernelBinaryCrossEntropy{
public:
    __aicore__ inline KernelBinaryCrossEntropy() {}
    __aicore__ inline void Init(GM_ADDR p, GM_ADDR y, GM_ADDR z, uint32_t totalLength, uint32_t tileNum)
    {
        this->blockLength = totalLength / AscendC::GetBlockNum();
        this->tileNum = tileNum;
        this->tileLength = this->blockLength / tileNum / BUFFER_NUM;

        pGm.SetGlobalBuffer((__gm__ DTYPE_P *)p + this->blockLength * AscendC::GetBlockIdx(), this->blockLength);
        yGm.SetGlobalBuffer((__gm__ DTYPE_Y *)y + this->blockLength * AscendC::GetBlockIdx(), this->blockLength);
        zGm.SetGlobalBuffer((__gm__ DTYPE_Z *)z + this->blockLength * AscendC::GetBlockIdx(), this->blockLength);        
        pipe.InitBuffer(inQueueP, BUFFER_NUM, this->tileLength * sizeof(DTYPE_P));
        pipe.InitBuffer(inQueueY, BUFFER_NUM, this->tileLength * sizeof(DTYPE_Y));
        pipe.InitBuffer(outQueueZ, BUFFER_NUM, this->tileLength * sizeof(DTYPE_Z));
        pipe.InitBuffer(tmpBuf0, this->tileLength * sizeof(DTYPE_P));
    }
    __aicore__ inline void Process()
    {
        int32_t loopCount = this->tileNum * BUFFER_NUM;
        for (int32_t i = 0; i < loopCount; i++) {
            CopyIn(i);
            Compute(i);
            CopyOut(i);
        }
    }

private:
    __aicore__ inline void CopyIn(int32_t progress)
    {
        AscendC::LocalTensor<DTYPE_P> pLocal = inQueueP.AllocTensor<DTYPE_P>();
        AscendC::LocalTensor<DTYPE_Y> yLocal = inQueueY.AllocTensor<DTYPE_Y>();
        AscendC::DataCopy(pLocal, pGm[progress * this->tileLength], this->tileLength);
        AscendC::DataCopy(yLocal, yGm[progress * this->tileLength], this->tileLength);
        inQueueP.EnQue(pLocal);
        inQueueY.EnQue(yLocal);
    }
    __aicore__ inline void Compute(int32_t progress)
    {
        AscendC::LocalTensor<DTYPE_P> pLocal = inQueueP.DeQue<DTYPE_P>();
        AscendC::LocalTensor<DTYPE_Y> yLocal = inQueueY.DeQue<DTYPE_Y>();
        AscendC::LocalTensor<DTYPE_Z> zLocal = outQueueZ.AllocTensor<DTYPE_Z>();
        AscendC::LocalTensor<DTYPE_P> tmpTensor0 = tmpBuf0.Get<DTYPE_P>();
        
        AscendC::Ln(zLocal, pLocal, this->tileLength);
        AscendC::Mul(zLocal, yLocal, zLocal, this->tileLength);
        AscendC::Duplicate(tmpTensor0, DTYPE_P(1.0f), this->tileLength);
        AscendC::Sub(pLocal, tmpTensor0, pLocal, this->tileLength);
        AscendC::Ln(pLocal, pLocal, this->tileLength);
        AscendC::Sub(yLocal, tmpTensor0, yLocal, this->tileLength);
        AscendC::MulAddDst(zLocal, yLocal, pLocal, this->tileLength);
        AscendC::Duplicate(tmpTensor0, DTYPE_P(0.0f), this->tileLength);
        AscendC::Sub(zLocal, tmpTensor0, zLocal, this->tileLength);

        outQueueZ.EnQue<DTYPE_Z>(zLocal);
        inQueueP.FreeTensor(pLocal);
        inQueueY.FreeTensor(yLocal);
    }
    __aicore__ inline void CopyOut(int32_t progress)
    {
        AscendC::LocalTensor<DTYPE_Z> zLocal = outQueueZ.DeQue<DTYPE_Z>();
        AscendC::DataCopy(zGm[progress * this->tileLength], zLocal, this->tileLength);
        outQueueZ.FreeTensor(zLocal);
    }

private:
    AscendC::TPipe pipe;
    AscendC::TQue<AscendC::TPosition::VECIN, BUFFER_NUM> inQueueP;
    AscendC::TQue<AscendC::TPosition::VECIN, BUFFER_NUM> inQueueY;
    AscendC::TQue<AscendC::TPosition::VECOUT, BUFFER_NUM> outQueueZ;

    AscendC::TBuf<AscendC::TPosition::VECCALC> tmpBuf0;

    AscendC::GlobalTensor<DTYPE_P> pGm;
    AscendC::GlobalTensor<DTYPE_Y> yGm;
    AscendC::GlobalTensor<DTYPE_Z> zGm;

    uint32_t blockLength;
    uint32_t tileNum;
    uint32_t tileLength;
};


extern "C" __global__ __aicore__ void binary_cross_entropy_custom(GM_ADDR p, GM_ADDR y, GM_ADDR z, GM_ADDR workspace, GM_ADDR tiling)
{
    GET_TILING_DATA(tiling_data, tiling);    
    KernelBinaryCrossEntropy op;
    op.Init(p, y, z, tiling_data.totalLength, tiling_data.tileNum);
    op.Process();
    

}